package org.ddd.book.infrastructure.base;

import lombok.AllArgsConstructor;
import org.ddd.book.domain.base.repository.IAddressRepository;
import org.ddd.book.domain.base.vo.AddressVO;
import org.ddd.book.domain.base.vo.CityVO;
import org.ddd.book.domain.base.vo.CountryVO;
import org.ddd.book.domain.base.vo.ProvinceVO;
import org.ddd.book.infrastructure.base.dao.CityMapperService;
import org.ddd.book.infrastructure.base.dao.CountryMapperService;
import org.ddd.book.infrastructure.base.dao.ProvinceMapperService;
import org.ddd.book.infrastructure.base.po.CityPO;
import org.ddd.book.infrastructure.base.po.CountryPO;
import org.ddd.book.infrastructure.base.po.ProvincePO;
import org.springframework.stereotype.Repository;

@Repository
@AllArgsConstructor
public class AddressRepositoryImpl implements IAddressRepository {
    private final CountryMapperService countryMapperService;
    private final ProvinceMapperService provinceMapperService;
    private final CityMapperService cityMapperService;

    @Override
    public AddressVO get(String countryCode, String provinceCode, String cityCode, String addressDetail) {
        CountryPO countryPO = countryMapperService.getById(countryCode);
        if (null == countryPO) {
            return null;
        }
        ProvincePO provincePO = provinceMapperService.getById(provinceCode);
        if (null == provincePO) {
            return null;
        }
        CityPO cityPO = cityMapperService.getById(cityCode);
        if (null == cityPO) {
            return null;
        }
        return new AddressVO(new CountryVO(countryPO.getCode(), countryPO.getName()), new ProvinceVO(provincePO.getCode(), provincePO.getName()), new CityVO(cityPO.getCode(), cityPO.getName()), addressDetail);
    }
}
